"use strict";
	/*
	//全局加载器v.0.2
	pm = {
		imgs: [
			{
				url: url,
				name: name
			},
			{
				url: url2,
				name: name2
			}
		],
		sprites: [
			{
				url: url,
				name: name
			}
		],
		//keyimgs实际是TD.preloadImgs的一个二次包装，参数详情参看preloadImgs方法
		keyimgs: [
			{
				el: elDom,
				pathPrefix: 'img/',
				postfix: 'jpg'
			}
		]
		ajaxs: [
			{
				url: 'xxx.html',
				type: 'POST',
				data: {},
				succback: function(data){
					console.log(data);
				},
				errback: function(msg){
					console.log(msg);
				}
			},
			{
				url: 'xxx.html'
			}
		]
	}
	
	var x = new Preload(param);
	x.onloading = function(p){
		console.log(p);
	}
	x.onload = function(){
		console.log('succback');
	}
	//默认只有ajax请求不成功才会触发
	x.onfail = function(err){
		console.log(err.url);
		console.log(err.msg);
	}
	//返回当前状态的百分比，完成是100
	x.getProcess();
	
	//启动
	x.load();
	
	加载完毕后Preload有一个全局的buffer，里面存了所有图片的键值对，格式如下例子
	Preload.buffer = {
		imgs: {
			bg: imgObj
		},
		sprites: {
			sp: imgObj
		},
		keyimgs: {
			title: TD.preloadImgs.buffer
		}
	}
	
	*/
	var TD = TD || {};
	
	var Preload = function(pm, keyW){
		var that = this;
		
		//预定义回调
		this.onloading = null;
		this.onload = null;
		this.onfail = null;
		
		//预定义全局变量
		var imgsLen = pm.imgs ? pm.imgs.length : 0;
		var spritesLen = pm.sprites ? pm.sprites.length : 0;
		var keyimgsLen = pm.keyimgs ? pm.keyimgs.length : 0;
		var ajaxsLen = pm.ajaxs ? pm.ajaxs.length : 0;
		var keyimgsWeight = keyW || 7;
		var totalLen = imgsLen + spritesLen + keyimgsLen * keyimgsWeight + ajaxsLen;
		var count = 0;
		
		this.getProcess = function(){
			return count;
		}
		
		//载入普通图片
		var loadImg = function(){
			var imgSuccessFn = function(e){
				count++;
				//存入内存中
				Preload.buffer.imgs[this.bufferName] = this;
				if (count == totalLen){
					that.onloading && that.onloading(100);
					that.onload && that.onload();
				}else {
					that.onloading && that.onloading(Math.round(count/totalLen * 100));
				}
			};
			
			for (var i = 0; i < imgsLen; i++) {
				var img = new Image();
				img.onload = img.onerror = imgSuccessFn;
				//加个属性
				img.bufferName = pm.imgs[i].name;
				img.src = pm.imgs[i].url;
			}
		};
		
		//载入雪碧图
		var loadSprite = function(){
			var imgSuccessFn = function(){
				count++;
				//存入内存中
				Preload.buffer.sprites[this.bufferName] = this;
				if (count == totalLen){
					that.onloading && that.onloading(100);
					that.onload && that.onload();
				}else {
					that.onloading && that.onloading(Math.round(count/totalLen * 100));
				}
			};
			
			for (var i = 0; i < spritesLen; i++) {
				var img = new Image();
				img.onload = img.onerror = imgSuccessFn;
				//加个属性
				img.bufferName = pm.sprites[i].name;
				img.src = pm.sprites[i].url;
			}
		};
		
		//载入帧动画图片
		var loadKeyimg = function(){
			var keyimgSuccessFn = function(){
				count = count + keyimgsWeight - this.keyProcess;
				Preload.buffer.keyimgs = TD.preloadImgs.buffer;
				if (count == totalLen) {
					that.onloading && that.onloading(100);
					that.onload && that.onload();
				}else {
					that.onloading && that.onloading(Math.round(count/totalLen * 100));
				}
			};
			
			var keyimgLoadFn = function(p){
				var c = Math.floor(p*keyimgsWeight/100);
				//如果没有变化就不执行
				if (c == this.keyProcess) {
					return;
				}
				var increase = c - this.keyProcess;
				count = count + increase;
				this.keyProcess = c;
				c !== keyimgsWeight && that.onloading && that.onloading(Math.round(count/totalLen * 100));
			};
			
			for (var i = 0; i < keyimgsLen; i++){
				var keyimg = new TD.preloadImgs(pm.keyimgs[i].el, pm.keyimgs[i].pathPrefix, pm.keyimgs[i].pad , pm.keyimgs[i].postfix);
				keyimg.onload = keyimgSuccessFn;
				keyimg.onloading = keyimgLoadFn;
				//加一个对象属性，记录进度
				keyimg.keyProcess = 0;
				keyimg.load();
			}
		};
		
		//载入ajax,加载器只负责加载资源，如果ajax载入不成功，也默认不去阻碍整体流程，但是可以在onfail中处理异常
		//ajax加载失败会在errback和onfail中同时被触发
		var loadAjax = function(){
			//计数器
			var ajaxSuccessFn = function(){
				count++;
				if (count == totalLen) {
					that.onloading && that.onloading(100);
					that.onload && that.onload();
				}else {
					that.onloading && that.onloading(Math.round(count/totalLen * 100));
				}
			};
			
			pm.ajaxs.forEach(function(value){
				TD.ajax({
					url: value.url,
					type: value.type || 'GET',
					data: value.data || '',
				}, (function(v){
					return function(data){
						ajaxSuccessFn();
						v.succback && v.succback(data);
					};
				})(value), (function(v){
					return function(msg){
						ajaxSuccessFn();
						v.errback && v.errback(msg);
						that.onfail && that.onfail({
							msg: msg,
							url: v.url
						});
					};
				})(value));
			});
		};
		
		this.load = function(){
			if (totalLen == 0) {
				this.onload && this.onload();
				return;
			}
			
			imgsLen !== 0 && loadImg();
			spritesLen !== 0 && loadSprite();
			keyimgsLen !== 0 && loadKeyimg();
			ajaxsLen !== 0 && loadAjax();
		};
	};
	Preload.buffer = {
		imgs: {},
		sprites: {},
		keyimgs: {}
	};
